//
//  ServerItemView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/3/9.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol ServerItemViewDelegate {
    func server_item_remove()
    func server_item_url(url:String)
}

class ServerItemView: UIView {
    
    private let titles = ["内网","外网"]
    private let urls = [SP_INNTER,SP_OUTTER]
    // 白色背景
    private let whiteView = UIView()
    
    // 代理
    public var delegate:ServerItemViewDelegate?
    
    override func layoutSubviews() {
        // 背景色
        backgroundColor = UIColor.init(displayP3Red: 0, green: 0, blue: 0, alpha: 0.4)
        // 白色背景
        whiteView.layer.cornerRadius = 8
        whiteView.backgroundColor = UIColor.white
        let whiteViewY = (frame.height - MARGIN100) * 0.5
        whiteView.frame = CGRect(x: MARGIN, y: whiteViewY, width: frame.width - MARGIN * 2, height: MARGIN140)
        addSubview(whiteView)
        // 内容宽度
        let lineW = whiteView.frame.width - MARGIN * 2
        // 高度
        var tempHeight:CGFloat = MARGIN10
        (0...1).forEach { (i) in
            let subItem = SubServerItemView()
            subItem.tag = i
            subItem.frame = CGRect(x: MARGIN, y: tempHeight, width: lineW, height: MARGIN60)
            subItem.resetUrl = urls[i]
            subItem.resetTitle = titles[i]
            whiteView.addSubview(subItem)
            // 记录高度
            tempHeight = subItem.frame.maxY
            // 下划线
            if i % 2 == 0 {
                let line = UIView(frame: CGRect(x: MARGIN, y: tempHeight, width: lineW, height: 1))
                line.backgroundColor = COLOR_LINE
                whiteView.addSubview(line)
            }
            // 点击事件
            subItem.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItemSelectUrl(ui:))))
        }
    }
    
    @objc func clickItemSelectUrl(ui:UITapGestureRecognizer) -> Void {
        if let index = ui.view?.tag {
            delegate?.server_item_url(url: urls[index])
        }
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else {
            return
        }
        if !whiteView.frame.contains(point) {
            delegate?.server_item_remove()
        }
    }
    
    class SubServerItemView: UIView {
        
        // 标题
        private let labelTitle = UILabel(title: "", color: COLOR_NICE, size: 15)
        // 地址
        private let labelUrl = UILabel(title: "", color: COLOR_NICE, size: 13)
        
        public var resetTitle:String? {
            didSet {
                labelTitle.text = resetTitle
            }
        }
        
        public var resetUrl:String? {
            didSet {
                labelUrl.text = resetUrl
            }
        }
        
        override func layoutSubviews() {
            // 标题
            labelTitle.frame = CGRect(x: 0, y: 0, width: MARGIN50, height: frame.height)
            addSubview(labelTitle)
            // 地址
            labelUrl.frame = CGRect(x: MARGIN50, y: 0, width: frame.width - MARGIN50, height: frame.height)
            addSubview(labelUrl)
        }
    }
}
